package control;

import control.bean.ControlBeanConstant;

import javax.swing.*;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/22/13
 * Time: 10:35 AM
 * To change this template use File | Settings | File Templates.
 */
public class Matrix extends Control {

    private static enum type {RADIO, CHECKBOX, TEXTFIELD};

    private type matrixType;
    private int column;
    private int row;
    private String[] columnText = { "text1" ,"text2" };
    private String[] rowText = { "test11", "text22"};

    public Matrix() {
        fieldType = ControlBeanConstant.MATRIX;
        matrixType = type.RADIO;
        row = 2;
        column = 2;

        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        controlPanel = new JPanel(new BorderLayout());
        controlPanel.setOpaque(false);

        label = new JTextField(super.labelText);
        controlPanel.add(label, BorderLayout.NORTH);

        controlPanel.add(buildMatrixTable(), BorderLayout.CENTER);

        return controlPanel;
    }
    //TODO what about button group.
    // only one selection per row
    //TODO jlabel, input not opaque
    //TODO add new row/column

    private Component buildMatrixTable() {
        JPanel matrixTable = new JPanel(new GridBagLayout());
        matrixTable.setOpaque(false);
        GridBagConstraints constraints = new GridBagConstraints();

        for ( int x = 0; x < row+1; x++ ) {
            for ( int y = 0; y < column+1; y++ ) {
                constraints.gridx = x;
                constraints.gridy = y;
                if ( x == 0 && y == 0 ) {/*do nothing*/
                } else if ( x == 0 ) {
                    matrixTable.add(new Label(columnText[y - 1]), constraints);
                } else if ( y == 0 ) {
                    matrixTable.add(new Label(rowText[x - 1]), constraints);
                } else {
                    JComponent input;
                    switch ( matrixType ) {
                        default:
                        case RADIO:
                            input = new JRadioButton();
                            break;
                        case CHECKBOX:
                            input = new JCheckBox();
                            break;
                        case TEXTFIELD:
                            input = new JTextField();
                            break;
                    }
                    input.setOpaque(false);
                    matrixTable.add(input, constraints);
                }
            }
        }

        controlPanel = new JPanel(new BorderLayout());
        controlPanel.setOpaque(false);
        label = new JTextField(labelText);

        controlPanel.add(label, BorderLayout.NORTH);
        controlPanel.add(matrixTable, BorderLayout.CENTER);

        return controlPanel;
    }

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel();

        return propertiesPanel;
    }
}
